package cn.fresh.xuanyuan.module.infra.controller.admin.column;

import cn.fresh.xuanyuan.framework.common.enums.ColumnTypeEnum;
import cn.fresh.xuanyuan.framework.common.exception.ErrorCode;
import cn.fresh.xuanyuan.framework.common.exception.ServiceException;
import cn.fresh.xuanyuan.framework.common.pojo.CommonResult;
import cn.fresh.xuanyuan.framework.common.pojo.PageParam;
import cn.fresh.xuanyuan.framework.common.pojo.PageResult;
import cn.fresh.xuanyuan.module.infra.dal.dataobject.column.MallColumnDO;
import cn.fresh.xuanyuan.module.infra.dal.dataobject.column.MallColumnProductDO;
import cn.fresh.xuanyuan.module.infra.dto.MallColumnDetailDto;
import cn.fresh.xuanyuan.module.infra.dto.MallColumnFormDto;
import cn.fresh.xuanyuan.module.infra.dto.MallColumnProductDto;
import cn.fresh.xuanyuan.module.infra.service.column.MallColumnProductService;
import cn.fresh.xuanyuan.module.infra.service.column.MallColumnService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Map;

@Tag(name = "管理后台 - 栏目管理")
@RestController
@RequestMapping("/infra/column")
@Validated
public class MallColumnController {

    @Autowired
    private MallColumnService mallColumnService;

    @Autowired
    private MallColumnProductService mallColumnProductService;

    @GetMapping(value = "/getPageList")
    @Operation(summary = "栏目列表")
    @PreAuthorize("@ss.hasPermission('infra:column:list')")
    public CommonResult<PageResult<MallColumnDO>> getPageList(@RequestParam(name = "name", required = false) String name,@RequestParam(name = "type", required = false) String type, @ModelAttribute PageParam pageParam) {
        return CommonResult.success(mallColumnService.getPageList(name, type, pageParam));
    }

    @PostMapping(value = "/add")
    @Operation(summary = "添加栏目")
    @PreAuthorize("@ss.hasPermission('infra:column:add')")
    public CommonResult<Boolean> add(@RequestBody @Valid MallColumnFormDto mallColumn) {
        if(!ColumnTypeEnum.wx.name().equalsIgnoreCase(mallColumn.getType()) && !ColumnTypeEnum.h5.name().equalsIgnoreCase(mallColumn.getType())) {
            throw new ServiceException(new ErrorCode(400, "栏目类型不正确"));
        }
        mallColumnService.addColumn(mallColumn);
        return CommonResult.success(true);
    }

    @PostMapping(value = "/update")
    @Operation(summary = "修改栏目")
    @PreAuthorize("@ss.hasPermission('infra:column:update')")
    public CommonResult<Boolean> update(@RequestBody @Valid MallColumnFormDto mallColumn) {
        if(!ColumnTypeEnum.wx.name().equalsIgnoreCase(mallColumn.getType()) && !ColumnTypeEnum.h5.name().equalsIgnoreCase(mallColumn.getType())) {
            throw new ServiceException(new ErrorCode(400, "栏目类型不正确"));
        }
        mallColumnService.updateColumn(mallColumn);
        return CommonResult.success(true);
    }

    @GetMapping(value = "/getColumnDetail")
    @Operation(summary = "栏目详情")
    @PreAuthorize("@ss.hasPermission('infra:column:detail')")
    public CommonResult<MallColumnDetailDto> getColumnDetail(@RequestParam(name = "id") Integer id) {
        return CommonResult.success(mallColumnService.getColumnDetail(id));
    }

    @GetMapping(value = "/getProductList")
    @Operation(summary = "栏目关联商品列表")
    @PreAuthorize("@ss.hasPermission('infra:column:productList')")
    public CommonResult<PageResult<MallColumnProductDO>> getProductList(@RequestParam(name = "columnId") Integer columnId, @RequestParam(name = "name", required = false) String name, @ModelAttribute PageParam pageParam) {
        return CommonResult.success(mallColumnProductService.getPageList(columnId, name, pageParam));
    }

    @PostMapping(value = "/addProduct")
    @Operation(summary = "添加栏目关联商品")
    @PreAuthorize("@ss.hasPermission('infra:column:productAdd')")
    public CommonResult<Boolean> addProduct(@RequestBody @Valid MallColumnProductDto mallColumnProduct) {
        mallColumnProductService.addColumnProduct(mallColumnProduct);
        return CommonResult.success(true);
    }

    @PostMapping(value = "/updateProduct")
    @Operation(summary = "修改栏目关联商品")
    @PreAuthorize("@ss.hasPermission('infra:column:productUpdate')")
    public CommonResult<Boolean> updateProduct(@RequestBody @Valid MallColumnProductDto mallColumnProduct) {
        mallColumnProductService.updateColumnProduct(mallColumnProduct);
        return CommonResult.success(true);
    }

    @PostMapping(value = "/deleteProduct")
    @Operation(summary = "删除栏目关联商品")
    @PreAuthorize("@ss.hasPermission('infra:column:productDelete')")
    public CommonResult<Boolean> deleteProduct(@RequestBody Map<String, Long> map) {
        if(map == null || map.get("id") == null) {
            return CommonResult.error(500,"参数不能为空");
        }
        mallColumnProductService.deleteColumnProduct(map.get("id"));
        return CommonResult.success(true);
    }
}
